package utgame.com.broadcastdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;

import utgame.com.contentpriverderdemo.ContentProviderExample;

/**
 * Created by xueal on 2018/1/26.
 */

public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] objs = (Object[]) intent.getExtras().get("pdus");
        for (Object obj : objs) {
            byte[] pdu = (byte[]) obj;
            SmsMessage sms = SmsMessage.createFromPdu(pdu);
            // 短信的内容
            String message = sms.getMessageBody();
            Log.d("TAG", "message=" + message);
            // 短信的发送方
            String from = sms.getOriginatingAddress();
            Log.d("TAG", "from=" + from);
//            analysisVerify(message);

//            ContentProviderExample.getSmsLog();

        }

    }

    /**
     * 解析短信并且回写，主要是提取出数字验证码并显示在输入框上
     *
     * @param message
     */
    private void analysisVerify(String message) {
        char[] msgs = message.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < msgs.length; i++) {
            if ('0' <= msgs[i] && msgs[i] <= '9') {
                sb.append(msgs[i]);
            }
        }
//        editText.setText(sb.toString());
    }
}
